﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Diagnostics;

namespace PDF多合一
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void HeBing_Click(object sender, EventArgs e)
        {            

            string[] lines = textBox1.Lines;
            if (lines.Length == 0)
            {
                MessageBox.Show("请先选择要合并的PDF文件！");
            }
            else
            {
                string filePath = textBox1.Lines[0];
                string directory = System.IO.Path.GetDirectoryName(filePath);
                string outputFilePath = directory + "\\" + WenJianMing.Text + ".pdf";
                Console.WriteLine("合并后的文件：" + outputFilePath);

                foreach (string line in lines)
                {
                    Console.WriteLine("参与合并的文件：" + line);
                }
                PDFMerger.MergePDFs(outputFilePath, lines);
                Process.Start(outputFilePath);
            }
                  
        }

        private void XuanWenJian_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in openFileDialog.FileNames)
                {
                    Console.WriteLine(file);
                    textBox1.AppendText(file + Environment.NewLine);
                }
            }
        }

        private void QingKong_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "C:\\Users\\15751\\Desktop\\贝拉合同.pdf";
            Process.Start(url);
        }
    }

    public class PDFMerger
    {
        public static void MergePDFs(string outputFilePath, params string[] filesToMerge)
        {
            using (FileStream stream = new FileStream(outputFilePath, FileMode.Create))
            {
                Document document = new Document();
                PdfCopy pdf = new PdfCopy(document, stream);
                PdfReader reader = null;
                try
                {
                    document.Open();
                    foreach (string file in filesToMerge)
                    {
                        reader = new PdfReader(file);
                        pdf.AddDocument(reader);
                        reader.Close();
                    }
                }
                catch (Exception ex)
                {
                    // Handle exception
                }
                finally
                {
                    if (document != null)
                    {
                        document.Close();
                    }
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }
        }
    }
}
